package com.swgk.core.util;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: 王彬臣
 * @date:
 * @Description: 输入文本框输入筛选工具类
 */
public class EditTextFilterUtils {
    /**
     * 过滤特殊字符和表情
     * @param et
     */

    public static void setEtFilter(EditText et) {
        if (et == null) {
            return;
        }
        //表情过滤器
        InputFilter emojiFilter = (source, start, end, dest, dstart, dend) -> {
            Pattern emoji = Pattern.compile(
                    "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                return "";
            }
            return null;
        };
        //特殊字符过滤器
        InputFilter specialCharFilter = (source, start, end, dest, dstart, dend) -> {
            String regexStr = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
            Pattern pattern = Pattern.compile(regexStr);
            Matcher matcher = pattern.matcher(source.toString());
            if (matcher.matches()) {
                return "";
            } else {
                return null;
            }

        };
        et.setFilters(new InputFilter[]{emojiFilter, specialCharFilter});
    }

    public static void setEtFilterAddLength(EditText et,int size) {
        if (et == null) {
            return;
        }
        //表情过滤器
        InputFilter emojiFilter = (source, start, end, dest, dstart, dend) -> {
            Pattern emoji = Pattern.compile(
                    "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                    Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
            Matcher emojiMatcher = emoji.matcher(source);
            if (emojiMatcher.find()) {
                return "";
            }
            return null;
        };
        //特殊字符过滤器
        InputFilter specialCharFilter = (source, start, end, dest, dstart, dend) -> {
            String regexStr = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
            Pattern pattern = Pattern.compile(regexStr);
            Matcher matcher = pattern.matcher(source.toString());
            if (matcher.matches()) {
                return "";
            } else {
                return null;
            }

        };


        et.setFilters(new InputFilter[]{emojiFilter, specialCharFilter,new InputFilter.LengthFilter(size)});
    }

    /**
     * 返回"" 禁止输入
     * 返回null 不限制输入
     */
    public static void getInputFilter(EditText editText, int digits) {
        editText.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
        // 控制输入框的小数位和长度,这里长度暂时设置为10
        editText.setFilters(new InputFilter[]{new InputFilter() {

            /**
             * @param source 用户当前输入的内容
             * @param start
             * @param end
             * @param dest 原有数据
             * @param dstart
             * @param dend
             * @return
             */
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                // 删除等特殊字符，直接返回
                if ("".equals(source.toString())) {
                    return null;
                }
                String dValue = dest.toString();
                if (TextUtils.isEmpty(dValue) && source.toString().equals(".")) {
                    return "0.";
                }
                Matcher m = Pattern.compile("[0-9]*").matcher(source);   //除数字外的其他的
                if (dValue.contains(".")) {
                    //已经存在小数点的情况下，只能输入数字
                    if (!m.matches()) {
                        return "";
                    }
                } else {
                    //未输入小数点的情况下，可以输入小数点和数字
                    if (!m.matches() && !source.equals(".")) {
                        return null;
                    }
                }
                String[] splitArray = dValue.split("\\.");
                if (splitArray.length > 1) {
                    String dotValue = splitArray[1];
                    int diff = dotValue.length() + 1 - digits;
                    if (diff > 0) {
                        return source.subSequence(start, end - diff);
                    }
                }
                return null;
            }
        }});
    }
    /**
     * 限制只能输入小数点后两位
     *
     * @param editText
     * @param s
     */
    public static void InputTwo(EditText editText, CharSequence s) {
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                s = s.toString().subSequence(0,
                        s.toString().indexOf(".") + 3);
                editText.setText(s);
                editText.setSelection(s.length());
            }
        }
        if (s.toString().trim().substring(0).equals(".")) {
            s = "0" + s;
            editText.setText(s);
            editText.setSelection(2);
        }
        if (s.toString().startsWith("0")
                && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                editText.setText(s.subSequence(0, 1));
                editText.setSelection(1);
                return;
            }
        }
    }
}
